package ATM;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


/**
 * GUI版本的ATM
 */
public class ATM extends JFrame {
    private ArrayList<User> users = new ArrayList();
    private Image bgImg = Util.getImage("/images/background.jpg");
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    // 登录成功的用户对象
    private User loginUser;

    public ATM() {
        init();
        this.setContentPane(new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg, 0, 0, this);
            }
        });
        this.setLayout(cardLayout);

        this.add(loginPanel, "login");
        this.add(menuPanel, "menu");
        this.add(searchPanel, "search");
        this.add(transferPanel, "transfer");
        this.add(getPanel, "get");//将卡片添加到ATM，关键字get
        this.add(savePanel, "save");


        this.setTitle("ATM");
        this.setSize(1024, 768);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    // 初始化用户集合
    public void init() {
//        users.add(new User("aaaaaa", "111111", 10000));
//        users.add(new User("bbbbbb", "222222", 20000));
//        users.add(new User("cccccc", "333333", 30000));
//        users.add(new User("dddddd", "444444", 40000));
//        users.add(new User("eeeeee", "555555", 50000));

        this.users = (ArrayList<User>) FileUtil.readObject("C:\\Users\\76618\\Desktop\\Java\\userData.dat");
    }


    public void switchPanel(String name) {
        FileUtil.writeObject("C:\\Users\\76618\\Desktop\\Java\\userData.dat",users);
        cardLayout.show(this.getContentPane(), name);
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }

    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public GetPanel getGetPanel() {
        return getPanel;
    }

    public void setGetPanel(GetPanel getPanel) {
        this.getPanel = getPanel;
    }

    public TransferPanel getTransferPanel() {
        return transferPanel;
    }

    public void setTransferPanel(TransferPanel transferPanel) {
        this.transferPanel = transferPanel;
    }

    public static void main(String[] args) {
        ATM atm = new ATM();


    }

}
